
package kaizone.haining.chaobohui.fragment;

import java.util.ArrayList;
import java.util.List;

import kaizone.haining.chaobohui.R;
import kaizone.haining.chaobohui.activity.CBHActivity;
import kaizone.haining.chaobohui.activity.DetailActivity;
import kaizone.haining.chaobohui.json.Data;
import kaizone.haining.chaobohui.json.SubjectItem;
import kaizone.haining.chaobohui.json.SubjectListDO;
import kaizone.haining.chaobohui.json.Item.PicModel;
import kaizone.haining.chaobohui.net.DataControl;
import kaizone.haining.chaobohui.util.ImageFetcher;
import kaizone.haining.chaobohui.widget.DetailGridImage;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ListView;

public class CBHGridViewFragment extends Fragment implements DataControl.IDataUpdateListener,
        OnItemClickListener {

    public static final String DATA_SOURCE_URL = "http://api.dp-app.com/chao/remote/rest/query_subject_list.htm";

    private List<SubjectItem> mData;

    private MyAdapter mAdapter;

    private DataControl mDataControl;

    private GridView mGridView;

    public CBHGridViewFragment() {
        mData = new ArrayList<SubjectItem>();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = (View) inflater.inflate(R.layout.cbh_fragment_gridview, null);
        mGridView = (GridView) view.findViewById(R.id.gridview);
        // imageView.setImageResource(R.drawable.x_zbg);
        mAdapter = new MyAdapter();
        mGridView.setAdapter(mAdapter);
        mGridView.setOnItemClickListener(this);
        mGridView.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {

                    @Override
                    public void onGlobalLayout() {
                        if (mAdapter == null)
                            return;
                        final int columnWidth = (mGridView.getWidth() / 2);
                        final int columnHeight = columnWidth * 2 / 3;
                        mAdapter.setItemHeight(columnHeight);

                    }
                });
        mDataControl = new DataControl(getActivity());
        mDataControl.addChangeListener(this);
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        mDataControl.exec(DATA_SOURCE_URL);
    }

    @Override
    public Object backParse(String datastring, String requeststring) {
        SubjectListDO subjectDO = SubjectListDO.parseJson(datastring);
        return subjectDO;
    }

    @Override
    public void foreUpdate(Object result, String requeststring) {
        if (result == null)
            return;
        if (result instanceof SubjectListDO) {
            SubjectListDO list = (SubjectListDO) result;
            if (list.success) {
                Data data = list.getData();
                if (data != null) {
                    List<SubjectItem> items = (List<SubjectItem>) data.getItems();
                    mData.removeAll(mData);
                    mData.addAll(items);
                    mAdapter.notifyDataSetChanged();
                }

            }
        }
    }

    @Override
    public void exception(Exception e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        SubjectItem tem = (SubjectItem) mData.get(position);
        String url = tem.getWebUrl();
        int itemid = tem.getId();
        DetailActivity.openSubject(getActivity(), url, itemid);
    }

    private class MyAdapter extends BaseAdapter {

        private int mItemHeight = 0;

        private GridView.LayoutParams mImageViewLayoutParams;

        public MyAdapter() {
            mImageViewLayoutParams = new GridView.LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.MATCH_PARENT);

        }

        @Override
        public int getCount() {
            return mData != null ? mData.size() : 0;
        }

        @Override
        public Object getItem(int position) {
            return mData != null ? mData.get(position) : 0;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            DetailGridImage gridImage = null;
            if (convertView == null) {
                gridImage = (DetailGridImage) View.inflate(getActivity(), R.layout.detail_grid_img,
                        null);
                gridImage.setLayoutParams(mImageViewLayoutParams);

                convertView = gridImage;
                // gridImage = (DetailGridImage) convertView;
                // int width = gridImage.getWidth();
                // int height = width * 2 / 3;
                // gridImage.setLayoutParams(new AbsListView.LayoutParams(width,
                // height));
            }
            gridImage = (DetailGridImage) convertView;

            if (gridImage.getLayoutParams().height != mItemHeight) {
                gridImage.setLayoutParams(mImageViewLayoutParams);
            }

            // test data...
            int size = mData.size();
            if (position < size) {
                SubjectItem item = (SubjectItem) mData.get(position);
                gridImage.setDescription(item.getName());

                List<PicModel> picModels = item.getPicModelList();
                if (picModels != null && !picModels.isEmpty()) {
                    PicModel picModel = picModels.get(0);
                    if (picModel != null) {
                        if (getActivity() instanceof CBHActivity) {
                            ImageFetcher imageFetcher = ((CBHActivity) getActivity())
                                    .getImageFetcher();
                            gridImage.loadImage(picModel.sum100x000, imageFetcher);
                        }
                    }
                }
            }
            return gridImage;
        }

        public void setItemHeight(int height) {
            if (height == mItemHeight) {
                return;
            }
            mItemHeight = height;
            mImageViewLayoutParams = new GridView.LayoutParams(LayoutParams.MATCH_PARENT,
                    mItemHeight);
            // mImageFetcher.setImageSize(height);
            notifyDataSetChanged();
        }

    }

    public static CBHGridViewFragment newInstance() {
        final CBHGridViewFragment f = new CBHGridViewFragment();
        return f;
    }

}
